package com.example.comsys.control;

import com.example.comsys.model.Page;
import com.example.comsys.model.Result;
import com.example.comsys.model.Wage;
import com.example.comsys.param.WageQueryParam;
import com.example.comsys.service.WageService;
import com.example.comsys.util.ComSysConstant;
import com.example.comsys.util.ResultUtils;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * wage api
 *
 * @author wjh
 * @date 2020/1/1, 周三
 */
@RestController
@RequestMapping(ComSysConstant.API_PREFIX + "/wage")
public class WageController {

  @Autowired
  private WageService wageService;

  @RequestMapping(value = "/list.json", method = {RequestMethod.GET, RequestMethod.POST})
  public Result<Page<Wage>> queryEmployees(@RequestBody WageQueryParam queryParam,
      @RequestParam(value = "limit", defaultValue = "true") boolean limit) {

    queryParam.setLimit(limit);
    Page<Wage> wagePage = wageService.queryDataByParam(queryParam);
    return ResultUtils.newSuccessfulResult(wagePage);
  }

  @GetMapping("/genWage.action")
  public Result<String> generateWage(int startYear, int startMonth, int endYear, int endMonth, long departId) {

    return wageService.rebuildWageInfo(startYear, startMonth, endYear, endMonth, departId);
  }

  @GetMapping("/mod.do")
  public Result<String> modWageInfo(long id, float benefitBonus, float deBonus,
      @RequestParam(required = false, defaultValue = "false") boolean deleteFlag) {

    Wage wage = new Wage();
    wage.setId(id);
    benefitBonus = (float) (Math.round(benefitBonus * 100) / 100);
    deBonus = (float) (Math.round(deBonus * 100) / 100);
    wage.setBenefitBonus(benefitBonus);
    wage.setDeBonus(deBonus);
    boolean b;
    if (deleteFlag) {
      b = wageService.deleteWage(id);
    } else {
      b = wageService.updateWageByWageId(wage);
    }
    return b ? ResultUtils.newSuccessfulResult() : ResultUtils.newFailedResult("更新失败");
  }


  @RequestMapping(value = "export.do", method = {RequestMethod.GET, RequestMethod.POST})
  public void exportExcel(@RequestParam(value = "filename", defaultValue = "wage") String filename,
      @RequestParam(value = "year", required = false) Integer year,
      @RequestParam(value = "month", required = false) Integer month,
      HttpServletResponse response) throws IOException {

    HSSFWorkbook workbook = wageService.generateExcel(filename, year, month);
    OutputStream ops = response.getOutputStream();
    response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls");
    response.setContentType("application/octet-stream");
    workbook.write(ops);

  }

}
